package com.suyun.controller;

import com.alibaba.fastjson.JSON;
import com.suyun.domain.Order;
import com.suyun.domain.Product;
import com.suyun.service.OrderService;
import com.suyun.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

/*@RestController*/
@Slf4j
public class OrdertController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;

    @Autowired
    private DiscoveryClient discoveryClient;

    //改用fegin
    @RequestMapping("/product/create/{pid}")
    public Order createOrder(@PathVariable("pid") Integer pid){
        log.info("创建一个pid为{}的订单",pid);

        log.info("rest",restTemplate);


        //直接使用服务名访问 service-producter
        Product product = productService.findByPid(pid);
        log.info("调用微服务查询商品为{}", JSON.toJSONString(product));


        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());

        order.setNumber(1);

        orderService.createOrder(order);
        log.info("创建订单成功{},pid={}",JSON.toJSONString(order),pid);
        return order;
    }

    /*@RequestMapping("/product/create/{pid}")
    public Order createOrder(@PathVariable("pid") Integer pid){
        log.info("创建一个pid为{}的订单",pid);

        log.info("rest",restTemplate);


        //直接使用服务名访问 service-producter
        Product product = restTemplate.getForObject("http://service-producter/product/" + pid, Product.class);
        log.info("调用微服务查询商品为{}", JSON.toJSONString(product));


        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());

        order.setNumber(1);

        orderService.createOrder(order);
        log.info("创建订单成功{},pid={}",JSON.toJSONString(order),pid);
        return order;
    }*/


    /*@RequestMapping("/product/create/{pid}")  手动实现负载均衡
    public Order createOrder(@PathVariable("pid") Integer pid){
        log.info("创建一个pid为{}的订单",pid);

        log.info("rest",restTemplate);

        List<ServiceInstance> instances = discoveryClient.getInstances("service-producter");
        //随机生成下标 实现负载均衡
        int index = new Random().nextInt(instances.size());
        ServiceInstance serviceInstance = instances.get(index);

        log.info("instance {}",JSON.toJSONString(serviceInstance));

        Product product = restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/product/" + pid, Product.class);
        log.info("调用微服务查询商品为{}", JSON.toJSONString(product));


        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());

        order.setNumber(1);

        orderService.createOrder(order);
        log.info("创建订单成功{},pid={}",JSON.toJSONString(order),pid);
        return order;
    }*/

    /*@RequestMapping("/product/create/{pid}")
    public Order createOrder(@PathVariable("pid") Integer pid){
        log.info("创建一个pid为{}的订单",pid);

        log.info("rest",restTemplate);

        List<ServiceInstance> instances = discoveryClient.getInstances("service-producter");
        ServiceInstance serviceInstance = instances.get(0);

        log.info("instance {}",JSON.toJSONString(serviceInstance));

        Product product = restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/product/" + pid, Product.class);
        log.info("调用微服务查询商品为{}", JSON.toJSONString(product));


        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());

        order.setNumber(1);

        orderService.createOrder(order);
        log.info("创建订单成功{},pid={}",JSON.toJSONString(order),pid);
        return order;
    }*/

    /*@RequestMapping("/product/create/{pid}")
    public Order createOrder(@PathVariable("pid") Integer pid){
        log.info("创建一个pid为{}的订单",pid);

        log.info("rest",restTemplate);

        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        log.info("调用微服务查询商品为{}", JSON.toJSONString(product));


        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());

        order.setNumber(1);

        orderService.createOrder(order);
        log.info("创建订单成功{},pid={}",JSON.toJSONString(order),pid);
        return order;
    }*/
}
